<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Poll/classes/PhpFox_ComponentPollList.class.php, $Author:  $ */



/** Component for showing list of polls
 *   
 * @version $Id: PhpFox_ComponentPollList.class.php 411 2008-03-18 17:01:03Z phpfoxsv $
 * @package module.poll
 */
class PhpFox_Mod_Poll_ComponentPollList extends BaseComponent
{
    function process()
    {
        $oPollItem = &$this->_oModule->getItem('Poll');
        /* @var $oPollItem PhpFox_Mod_Poll_ItemPoll */
        $oPollService = &$this->_oModule->getService('Poll');
        /* @var $oPollService PhpFox_Mod_Poll_ServicePoll */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        $oAccountService = &App::getModuleService('Account', 'Account');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oTpl = &$this->_oModule->getTemplate();
        $oUrl = &App::getUrl();
        $oReq = &App::getRequest();
        App::loadClass('Pager');

        $oSecurityService->checkPerm('poll', array('read_only', 'full'));
        $oSecurityService->checkAccess('poll');
        $sUser = $oSecurityService->getCurrentUserLogin();
        $bAdmin = $oSecurityService->isStaff();
        if ($iPollId = $oReq->getInt('delete'))
        {
            if (!$bAdmin)
            {
                App::gotoUrl('public.poll');
            }
            $oPollItem->load($iPollId);
            $oPollService->deletePoll($iPollId);
            $oAccountService->changeUserInfo($oAccountService->getByUsername($oPollItem->aData['user']), 'poll', -1);
            $oUrl->clearParams('delete');
            App::forward($oUrl->getUrl());
        }
        $iPage = $oReq->get('page', 1);
        $iPageSize = App::getParam('public.poll_list_count');
        $aCond = array();
        $aCond['type'] = '= 0';
        $sSort = 'time DESC';
        list($aItems, $iCnt) = $oPollItem->getList($aCond, $iPage, $iPageSize, $sSort);
        foreach ($aItems as $iKey => $aItem)
        {
            $aItems[$iKey]['answer'] = $oPollService->isUserCanVote($aItem['id']);
            $aItems[$iKey]['can_view'] = (App::isStaff() ? true : ($aItems[$iKey]['answer'] || ($aItem['user'] == $sUser)) ? true : false);
            $aItems[$iKey]['color']  = (($iKey % 4 <= 2) && ($iKey % 4 != 0)) ? true : false;
        }
        $oPager = &new Pager($iCnt, $iPage, $iPageSize);
        $iFrom = $oPager->getOffset($iPage, $iPageSize);
        $iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

        $oTpl->assign(array(
            'aItems'     => $aItems,
            'iCnt'       => $iCnt,
            'iTo'        => $iTo,
            'bAdmin'     => $bAdmin,
            'iFrom'      => ($iCnt) ? ($iFrom + 1) : 0,
            'sUImageUrl' => App::getParam('user.pic.url'),
            'aPaging'    => ($iCnt) ? $oPager->getInfo($oUrl) : '',
        ));
        return $oTpl->fetch('PollList.html');
    }
}
?>